
import ResManager from "../manager/ResManager";
import { UIID } from "../ui/UIConst";
import { UIManager } from "../ui/UIManager";
import HotUpdate, {HotOptions} from "./HotUpdate";

const {ccclass, property} = cc._decorator;

@ccclass
export default class HotUpdateComponent extends cc.Component {
    @property({displayName: 'project.manifest', type: cc.Asset,})
    manifest: cc.Asset = null;

    @property({displayName: '版本号', type: cc.Label,})
    versionLabel: cc.Label = null;

    @property({displayName: '热更新进度条', type: cc.ProgressBar})
    updateProgress: cc.ProgressBar = null;

    @property({displayName: '消息提示', type: cc.Label})
    tipsLabel: cc.Label = null;

  

    onLoad() {
        this._initView();
        UIManager.instance.init();
        let options = new HotOptions();
        options.OnVersionInfo = (data) => {
            let {local, server} = data;
            this.versionLabel.string = `本地版本号:${local}, 服务器版本号:${server}`;
        };
        options.OnUpdateProgress = (event: jsb.EventAssetsManager) => {
            let bytes = event.getDownloadedBytes() + '/' + event.getTotalBytes();
            let files = event.getDownloadedFiles() + '/' + event.getTotalFiles();

            let file = event.getPercentByFile().toFixed(2);
            let byte = event.getPercent().toFixed(2);
            let msg = event.getMessage();

            console.log('[update]: 进度=' + file);
            this.updateProgress.progress = parseFloat(file);
            this.tipsLabel.string = '正在更新中,请耐心等待';
            console.log(msg);
        };
        options.OnNeedToUpdate = (data) => {
            UIManager.instance.open(UIID.Dialog,'检测到新版本,点击确定开始更新', () => {
                HotUpdate.hotUpdate();
            });
        };
        options.OnNoNeedToUpdate = () => {
            this._enterGame();
        };
        options.OnUpdateFailed = () => {
            this.tipsLabel.string = '更新失败';
            console.log('热更新失败');
            UIManager.instance.open(UIID.Dialog,'更新失败,点击重试', () => {
                HotUpdate.checkUpdate();
            });

        };
        options.OnUpdateSucceed = () => {
            this.tipsLabel.string = '更新成功';
            console.log('更新成功');
            UIManager.instance.open(UIID.Dialog,'更新成功,点击确定重启游戏', () => {
                cc.audioEngine.stopAll();
                cc.game.restart();
            });
        };
        HotUpdate.init(this.manifest, options);
        this.onBtnClickCheckUpdate();
    }


    _initView() {
        this.tipsLabel.string = '';
        this.versionLabel.string = '';
        this.updateProgress.progress = 0;
    }

    // 检查更新
    onBtnClickCheckUpdate() {
        if (cc.sys.isNative) {
            if (this.manifest) {
                this.tipsLabel.string = '正在获取版本...';
                HotUpdate.checkUpdate();
            }
        } else {
            console.log('web 平台不需要热更新');
            this._enterGame();
        }
    }

    _enterGame() {
        console.log('进入游戏成功');
        //this.updateProgress.node.active = false;
        //cc.director.loadScene('GameScene');
        this.load();
    }

    async load(){
        let anims = ResManager.instance.loadDir('atlas',(c,t)=>{
            this.updateProgress.progress = c/t;
        });
        let prefab = ResManager.instance.loadDir<cc.Prefab>('model');
        await Promise.all([anims,prefab]);
        cc.director.loadScene('game');
    }
}
